import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from pgmpy.models import BayesianNetwork
from scipy.stats import mannwhitneyu
from pgmpy.estimators import BayesianEstimator
from pgmpy.inference import VariableElimination
from sklearn.preprocessing import LabelEncoder
from sklearn.linear_model import LogisticRegression
from scipy.stats import fisher_exact, chi2_contingency
from sklearn.neighbors import NearestNeighbors
from itertools import product
import os
import warnings

warnings.filterwarnings("ignore")

# Load cleaned survey data
file_path = r"D:\RENJIN RAJU\MASTERS\SEMESTER 3\THESIS\Research\Analysis\Survey data cleaned.csv"
df = pd.read_csv(file_path, encoding="ISO-8859-1")

# Define relevant technology and disruption columns
tech_cols = [
    'adoption_Artificial_Intelligence_AI', 'adoption_Internet_of_Things_IoT',
    'adoption_Blockchain', 'adoption_Cloud_Computing',
    'adoption_Robotic_and_Automation', 'adoption_Big_Data_Analytics',
    'adoption_Simulation'
]

disruption_cols = [
    'Disruption_Global_Crises', 'Disruption_Supplier_Failures',
    'Disruption_Natural_Disasters', 'Disruption_Transportation_Delays',
    'Disruption_Cybersecurity_Threats'
]

# Prepare DataFrame for Bayesian Network
df_bn = df[disruption_cols + tech_cols].copy()
df_bn = df_bn.fillna(0)
df_bn[disruption_cols + tech_cols] = df_bn[disruption_cols + tech_cols].applymap(lambda x: 1 if x > 0 else 0)

# Define Bayesian network structure
edges = [(disruption, tech) for disruption, tech in product(disruption_cols, tech_cols)]
model = BayesianNetwork(edges)

# Fit the model using Bayesian Estimator
model.fit(df_bn, estimator=BayesianEstimator, prior_type="BDeu")

# Perform inference
inference = VariableElimination(model)

# Query conditional probabilities
query_results = []
for tech in tech_cols:
    for disruption in disruption_cols:
        try:
            prob = inference.query(variables=[tech], evidence={disruption: 1}).values[1]
            query_results.append({
                "Query": f"P({tech}=1 | {disruption}=1)",
                "Result": round(prob, 4)
            })
        except Exception as e:
            query_results.append({
                "Query": f"P({tech}=1 | {disruption}=1)",
                "Result": f"Error: {str(e)}"
            })

# Convert results to DataFrame
query_df = pd.DataFrame(query_results)

# Create heatmap matrix
heatmap_matrix = pd.DataFrame(index=tech_cols, columns=disruption_cols)
for row in query_results:
    if isinstance(row['Result'], float):
        tech = row['Query'].split('=1 |')[0].split('P(')[1]
        disruption = row['Query'].split('| ')[1].replace('=1)', '')
        heatmap_matrix.loc[tech, disruption] = row['Result']

# Convert to float for plotting
heatmap_matrix = heatmap_matrix.astype(float)

# Plot heatmap
plt.figure(figsize=(12, 8))
sns.heatmap(heatmap_matrix, annot=True, fmt=".2f", cmap="YlGnBu", cbar_kws={'label': 'P(Technology Adoption | Disruption)'})
plt.title("Conditional Probability of Technology Adoption Given Disruption (Bayesian Inference)")
plt.xlabel("Disruption Type")
plt.ylabel("Technology")
plt.tight_layout()
plt.savefig(r"D:\RENJIN RAJU\MASTERS\SEMESTER 3\THESIS\Research\Analysis\scenario modelling\bayesian_disruption_tech_heatmap.png")

query_df.to_csv(r"D:\RENJIN RAJU\MASTERS\SEMESTER 3\THESIS\Research\Analysis\scenario modelling\bayesian_disruption_query_results.csv", index=False)

query_df.head()



barrier_cols = [
    'barriers_High_Implementation_Costs',
    'barriers_Lack_of_Skilled_Workforce',
    'barriers_Integration_Issues',
    'barriers_Resistance_to_Change'
]

# Count '1's (Yes) for each barrier
barrier_counts = df[barrier_cols].sum()

# Calculate percentages
total_responses = len(df)
barrier_percent = (barrier_counts / total_responses) * 100

# Create labels with percentages
labels = [
    f"High Costs\n{barrier_percent[0]:.1f}%",
    f"Skill Gaps\n{barrier_percent[1]:.1f}%",
    f"Integration\n{barrier_percent[2]:.1f}%",
    f"Resistance\n{barrier_percent[3]:.1f}%"
]

# Colors for the pie chart
colors = ['#ff9999','#66b3ff','#99ff99','#ffcc99']

# Create pie chart
plt.figure(figsize=(8, 6))
plt.pie(barrier_percent,
        labels=labels,
        colors=colors,
        autopct='%1.1f%%',
        startangle=90,
        pctdistance=0.85)

# Draw circle to make it a donut chart (optional)
centre_circle = plt.Circle((0,0), 0.70, fc='white')
fig = plt.gcf()
fig.gca().add_artist(centre_circle)

# Equal aspect ratio ensures pie is drawn as circle
plt.axis('equal')
plt.title('Barriers to Technology Implementation', pad=20)

# Adjust layout to prevent label cutoff
plt.tight_layout()
plt.show()


label_encoders = {}
for col in ['Industry', 'role']:  # Add other categorical columns if needed
    le = LabelEncoder()
    df[col] = le.fit_transform(df[col].astype(str))
    label_encoders[col] = le

tech_columns = {
    'AI': 'adoption_Artificial_Intelligence_AI',
    'BDA': 'adoption_Big_Data_Analytics',
    'IoT': 'adoption_Internet_of_Things_IoT',
    'Cloud': 'adoption_Cloud_Computing',
    'Blockchain': 'adoption_Blockchain',
    'Robotics': 'adoption_Robotic_and_Automation'
}
for tech, col in tech_columns.items():
    df[f'adopt_{tech}'] = df[col].apply(lambda x: 1 if x == 1 else 0)

# 1. Visual Analysis
plt.figure(figsize=(18, 12))

# Technology adoption mosaic plot
plt.subplot(2, 2, 1)
adoption_rates = df[[f'adopt_{tech}' for tech in tech_columns]].mean().sort_values()
sns.barplot(x=adoption_rates.values, y=adoption_rates.index, palette='coolwarm')
plt.title('Technology Adoption Rates (Overall Sample)')
plt.xlabel('Proportion of Companies Adopting')

# Adoption by skill barrier presence
plt.subplot(2, 2, 2)
skill_barrier = df['barriers_Lack_of_Skilled_Workforce']
tech_adoption = df[[f'adopt_{tech}' for tech in tech_columns]].mean(axis=1)
sns.kdeplot(x=tech_adoption, hue=skill_barrier, multiple='fill', palette='viridis')
plt.title('Technology Adoption Distribution by Skill Barrier')
plt.xlabel('Proportion of Technologies Adopted')
plt.ylabel('Density')

# Heatmap of adoption differences
plt.subplot(2, 2, 3)
effect_sizes = []
for tech in tech_columns:
    effect = (df[df['barriers_Lack_of_Skilled_Workforce'] == 1][f'adopt_{tech}'].mean() -
              df[df['barriers_Lack_of_Skilled_Workforce'] == 0][f'adopt_{tech}'].mean())
    effect_sizes.append(effect)

sns.heatmap(pd.DataFrame([effect_sizes], columns=tech_columns.keys()),
            annot=True, cmap='RdBu', center=0,
            yticklabels=['Adoption Difference'])
plt.title('Adoption Rate Differences (Skill Barrier vs No Barrier)')
plt.ylabel('')

plt.tight_layout()
plt.show()


# 2. Propensity Score Matching (Non-parametric version)
def perform_psm(data, treatment_col, outcome_col, covariates):
    # Estimate propensity scores
    ps_model = LogisticRegression()
    ps_model.fit(data[covariates], data[treatment_col])
    data['propensity_score'] = ps_model.predict_proba(data[covariates])[:, 1]

    # Nearest neighbor matching
    treated = data[data[treatment_col] == 1]
    control = data[data[treatment_col] == 0]
    if len(treated) == 0 or len(control) == 0:
        print(f"No matches found for {treatment_col}")
        return None, None, None
    nbrs = NearestNeighbors(n_neighbors=1).fit(control[['propensity_score']])
    distances, indices = nbrs.kneighbors(treated[['propensity_score']])

    matched_control = control.iloc[indices.flatten()]
    matched_data = pd.concat([treated, matched_control])

    # Calculate treatment effect
    effect = matched_data[matched_data[treatment_col] == 1][outcome_col].mean() - \
             matched_data[matched_data[treatment_col] == 0][outcome_col].mean()

    # Mann-Whitney U test for significance
    stat, p_val = mannwhitneyu(
        matched_data[matched_data[treatment_col] == 1][outcome_col],
        matched_data[matched_data[treatment_col] == 0][outcome_col],
        alternative='two-sided'
    )

    return effect, p_val, matched_data


# Define covariates
covariates = ['size', 'Industry', 'barriers_High_Implementation_Costs',
              'barriers_Integration_Issues', 'barriers_Resistance_to_Change']

# Run PSM for all technologies
results = []
for tech in tech_columns:
    try:
        effect, p_val, matched = perform_psm(
            df.dropna(subset=covariates + ['barriers_Lack_of_Skilled_Workforce', f'adopt_{tech}']),
            treatment_col='barriers_Lack_of_Skilled_Workforce',
            outcome_col=f'adopt_{tech}',
            covariates=covariates
        )
        results.append({
            'Technology': tech,
            'Effect Size': effect,
            'Mann-Whitney p-value': p_val,
            'Treated Adoption': matched[matched['barriers_Lack_of_Skilled_Workforce'] == 1][f'adopt_{tech}'].mean(),
            'Control Adoption': matched[matched['barriers_Lack_of_Skilled_Workforce'] == 0][f'adopt_{tech}'].mean(),
            'N (Treated)': sum(matched['barriers_Lack_of_Skilled_Workforce'] == 1),
            'N (Control)': sum(matched['barriers_Lack_of_Skilled_Workforce'] == 0)
        })
    except Exception as e:
        print(f"Error processing {tech}: {str(e)}")

if results:
    results_df = pd.DataFrame(results)

    # Create significance indicator using correct column name
    results_df['Significant'] = results_df['Mann-Whitney p-value'] < 0.05

    print("\nPSM Results with Mann-Whitney U Test:")
    print(results_df.to_markdown(index=False))

    # Visualization
    plt.figure(figsize=(12, 6))
    sns.scatterplot(
        data=results_df,
        x='Effect Size',
        y='Technology',
        size='N (Treated)',
        hue='Significant',
        palette={True: 'red', False: 'gray'},
        sizes=(50, 200),
        legend='brief'
    )
    plt.axvline(x=0, color='black', linestyle='--')
    plt.title('Skill Barrier Effect on Technology Adoption')
    plt.xlabel('Adoption Rate Difference (Treated - Control)')
    plt.ylabel('Technology')
    plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
    plt.tight_layout()
    plt.show()
else:
    print("No valid PSM results were generated")


tech_columns = {
    'IoT': 'adoption_Internet_of_Things_IoT',
    'BDA': 'adoption_Big_Data_Analytics'
}

# Create binary adoption indicators
for tech, col in tech_columns.items():
    df[f'adopt_{tech}'] = df[col].apply(lambda x: 1 if x == 1 else 0)

# Create composite responsiveness score (modify based on your actual metrics)
responsive_cols = ['effect_of_technologies_in real_time_data',
                  'visibility_and_control_Rank_1',
                  'resilience_collaboration_IoT_for_real-time_tracking']

# Convert all columns to numeric, coercing errors to NaN
for col in responsive_cols:
    df[col] = pd.to_numeric(df[col], errors='coerce')

# Now calculate the mean, skipping NaN values
df['responsiveness_score'] = df[responsive_cols].mean(axis=1, skipna=True)

# Optionally fill NaN values if needed
df['responsiveness_score'] = df['responsiveness_score'].fillna(0)

# 1. Visual Analysis
plt.figure(figsize=(18, 12))

# Panel 1: Responsiveness distribution by technology adoption
plt.subplot(2, 2, 1)
sns.boxplot(data=df.melt(id_vars='responsiveness_score',
                         value_vars=[f'adopt_{tech}' for tech in tech_columns],
                         var_name='Technology',
                         value_name='Adopted'),
            x='Technology', y='responsiveness_score', hue='Adopted',
            palette={0: 'red', 1: 'green'})
plt.title('Supply Chain Responsiveness by Technology Adoption')
plt.ylabel('Responsiveness Score')
plt.xlabel('')
plt.legend(title='Adoption Status')

# Panel 2: Adoption combinations heatmap
plt.subplot(2, 2, 2)
adoption_counts = df.groupby(['adopt_IoT', 'adopt_BDA'])['responsiveness_score'].mean().unstack()
sns.heatmap(adoption_counts, annot=True, fmt='.2f', cmap='YlGnBu')
plt.title('Average Responsiveness by Technology Combination')
plt.xlabel('BDA Adoption')
plt.ylabel('IoT Adoption')
plt.xticks([0.5, 1.5], ['No', 'Yes'])
plt.yticks([0.5, 1.5], ['No', 'Yes'])

# Panel 3: Responsiveness vs adoption intensity
plt.subplot(2, 2, 3)
tech_adoption = df[[f'adopt_{tech}' for tech in tech_columns]].sum(axis=1)
sns.regplot(x=tech_adoption, y=df['responsiveness_score'],
            x_jitter=0.1, scatter_kws={'alpha': 0.4})
plt.title('Responsiveness vs Number of Technologies Adopted')
plt.xlabel('Count of Adopted Technologies (IoT + BDA)')
plt.ylabel('Responsiveness Score')

plt.tight_layout()
plt.show()


# 2. Propensity Score Matching Analysis
def perform_psm(data, treatment_col, outcome_col, covariates):
    # Estimate propensity scores
    ps_model = LogisticRegression()
    ps_model.fit(data[covariates], data[treatment_col])
    data['propensity_score'] = ps_model.predict_proba(data[covariates])[:, 1]

    # Nearest neighbor matching
    treated = data[data[treatment_col] == 1]
    control = data[data[treatment_col] == 0]

    nbrs = NearestNeighbors(n_neighbors=1).fit(control[['propensity_score']])
    distances, indices = nbrs.kneighbors(treated[['propensity_score']])

    matched_control = control.iloc[indices.flatten()]
    matched_data = pd.concat([treated, matched_control])

    # Calculate treatment effect
    effect = matched_data[matched_data[treatment_col] == 1][outcome_col].mean() - \
             matched_data[matched_data[treatment_col] == 0][outcome_col].mean()

    # Mann-Whitney U test
    stat, p_val = mannwhitneyu(
        matched_data[matched_data[treatment_col] == 1][outcome_col],
        matched_data[matched_data[treatment_col] == 0][outcome_col],
        alternative='two-sided'
    )

    return effect, p_val, matched_data


# Define covariates (adjust based on your data)
covariates = ['size', 'Industry', 'barriers_High_Implementation_Costs',
              'barriers_Lack_of_Skilled_Workforce', 'Disruption_Global_Crises']

# Run PSM for each technology
results = []
for tech in tech_columns:
    try:
        effect, p_val, matched = perform_psm(
            df.dropna(subset=covariates + [f'adopt_{tech}', 'responsiveness_score']),
            treatment_col=f'adopt_{tech}',
            outcome_col='responsiveness_score',
            covariates=covariates
        )
        results.append({
            'Technology': tech,
            'Effect Size': effect,
            'Mann-Whitney p-value': p_val,
            'Adopters Mean': matched[matched[f'adopt_{tech}'] == 1]['responsiveness_score'].mean(),
            'Non-Adopters Mean': matched[matched[f'adopt_{tech}'] == 0]['responsiveness_score'].mean(),
            'N (Adopters)': sum(matched[f'adopt_{tech}'] == 1),
            'N (Non-Adopters)': sum(matched[f'adopt_{tech}'] == 0)
        })
    except Exception as e:
        print(f"Error processing {tech}: {str(e)}")

# Convert results to DataFrame
results_df = pd.DataFrame(results)
print("\nPSM Results for Responsiveness Impact:")
print(results_df.to_markdown(index=False))

# 3. Combination Effects Analysis
# Create interaction groups
df['tech_combo'] = np.where(
    (df['adopt_IoT'] == 1) & (df['adopt_BDA'] == 1), 'Both',
    np.where(df['adopt_IoT'] == 1, 'IoT Only',
             np.where(df['adopt_BDA'] == 1, 'BDA Only', 'Neither')))

plt.figure(figsize=(10, 6))
sns.boxplot(data=df, x='tech_combo', y='responsiveness_score',
            order=['Neither', 'IoT Only', 'BDA Only', 'Both'],
            palette='Set2')
plt.title('Responsiveness by Technology Combination')
plt.xlabel('Technology Adoption Group')
plt.ylabel('Responsiveness Score')
plt.show()

# Kruskal-Wallis test for group differences
from scipy.stats import kruskal

groups = [group[1]['responsiveness_score'].values
          for group in df.groupby('tech_combo')]
h_stat, p_val = kruskal(*groups)
print(f"\nKruskal-Wallis Test p-value: {p_val:.4f}")



# visibility_cols = [col for col in df.columns if 'visibility_and_control_Rank' in col]
#
# # Create binary adoption columns
# df['IoT_adopted'] = df['adoption_Internet_of_Things_IoT'] == 1
# df['Blockchain_adopted'] = df['adoption_Blockchain'] == 1
#
# # Create a combined visibility score based on presence of specific terms
# visibility_keywords = [
#     'Real-Time', 'Tracking', 'Monitoring', 'Automated',
#     'Scenario', 'Simulation', 'Decision', 'Compliance'
# ]
#
#
# # Function to detect visibility keywords
# def contains_visibility_keywords(text):
#     if pd.isna(text) or text == 'N/A':
#         return 0
#     return int(any(keyword.lower() in str(text).lower() for keyword in visibility_keywords))
#
#
# # Create visibility indicators for each rank column
# for col in visibility_cols:
#     df[f'{col}_visibility'] = df[col].apply(contains_visibility_keywords)
#
# # Create overall visibility score (count of visibility methods mentioned)
# df['visibility_score'] = df[[f'{col}_visibility' for col in visibility_cols]].sum(axis=1)
#
# # Analysis ----------------------------------------------------------------
#
# print("\n=== Visibility Method Analysis ===\n")
#
# # 1. Compare visibility methods between adopters and non-adopters
# tech_list = ['IoT', 'Blockchain']
#
# for tech in tech_list:
#     adoption_col = f'{tech}_adopted'
#
#     # Create contingency table
#     contingency = pd.crosstab(df[adoption_col], df['visibility_score'] > 0)
#
#     # Fisher's exact test for small samples
#     if contingency.size == 4:  # Only run if 2x2 table
#         oddsratio, p_value = fisher_exact(contingency)
#         print(f"{tech} Adoption vs. Visibility Methods:")
#         print(contingency)
#         print(f"Fisher's Exact Test p-value: {p_value:.4f}")
#         print(f"Odds Ratio: {oddsratio:.2f}\n")
#     else:
#         # Chi-square test for larger tables
#         chi2, p_value, _, _ = chi2_contingency(contingency)
#         print(f"{tech} Adoption vs. Visibility Methods:")
#         print(contingency)
#         print(f"Chi-square Test p-value: {p_value:.4f}\n")
#
# # 2. Compare mean visibility scores between groups
# print("\n=== Mean Visibility Scores ===")
# print(df.groupby('IoT_adopted')['visibility_score'].describe())
# print("\n")
# print(df.groupby('Blockchain_adopted')['visibility_score'].describe())
#
# # Visualization ------------------------------------------------------------
#
# plt.figure(figsize=(15, 5))
#
# # 1. Adoption vs Visibility Presence
# plt.subplot(1, 3, 1)
# sns.countplot(data=df, x='IoT_adopted', hue='visibility_score' > 0)
# plt.title('IoT Adoption vs. Visibility Methods')
# plt.xlabel('IoT Adopted')
# plt.ylabel('Count')
#
# plt.subplot(1, 3, 2)
# sns.countplot(data=df, x='Blockchain_adopted', hue='visibility_score' > 0)
# plt.title('Blockchain Adoption vs. Visibility Methods')
# plt.xlabel('Blockchain Adopted')
# plt.ylabel('Count')
#
# # 2. Visibility Score Distribution
# plt.subplot(1, 3, 3)
# sns.boxplot(data=df, x='visibility_score', y='visibility_score' > 0)
# plt.title('Distribution of Visibility Scores')
# plt.xlabel('Visibility Score')
# plt.ylabel('Has Visibility Methods')
#
# plt.tight_layout()
# plt.show()
#
# # 3. Top Visibility Methods
# print("\n=== Most Common Visibility Methods ===")
# all_methods = pd.concat([df[col] for col in visibility_cols])
# top_methods = all_methods.value_counts().head(10)
# print(top_methods)


visibility_cols = [col for col in df.columns if 'visibility_and_control_Rank' in col]

# Convert all visibility columns to strings and clean them
for col in visibility_cols:
    df[col] = df[col].astype(str).replace({'N/A': np.nan, 'nan': np.nan})

# Create binary adoption columns (0/1)
df['IoT_adopted'] = df['adoption_Internet_of_Things_IoT'].astype(int)
df['Blockchain_adopted'] = df['adoption_Blockchain'].astype(int)

# Create visibility indicator (1 if any visibility method is mentioned)
df['has_visibility'] = 0
for col in visibility_cols:
    df['has_visibility'] = np.where(
        df[col].notna() & (df[col] != ''),
        1,
        df['has_visibility']
    )

# Analysis ----------------------------------------------------------------

print("\n=== Visibility Method Analysis ===\n")

# 1. Compare visibility methods between adopters and non-adopters
tech_list = ['IoT', 'Blockchain']

for tech in tech_list:
    adoption_col = f'{tech}_adopted'

    # Create contingency table
    contingency = pd.crosstab(df[adoption_col], df['has_visibility'])

    print(f"{tech} Adoption vs. Visibility Methods:")
    print(contingency)

    # Fisher's exact test for 2x2 tables
    if contingency.shape == (2, 2):
        oddsratio, p_value = fisher_exact(contingency)
        print(f"Fisher's Exact Test p-value: {p_value:.4f}")
        print(f"Odds Ratio: {oddsratio:.2f}\n")
    else:
        print("Not enough data for Fisher's exact test\n")

# 2. Count of visibility methods by adoption status
print("\n=== Visibility Method Counts ===")
print(df.groupby(['IoT_adopted', 'Blockchain_adopted'])['has_visibility'].value_counts())

# Visualization ------------------------------------------------------------

plt.figure(figsize=(12, 5))

# 1. Adoption vs Visibility Presence
plt.subplot(1, 2, 1)
sns.countplot(
    data=df,
    x='IoT_adopted',
    hue='has_visibility',
    palette=['red', 'green']
)
plt.title('IoT Adoption vs Visibility Methods')
plt.xlabel('IoT Adopted (0=No, 1=Yes)')
plt.ylabel('Count')
plt.legend(title='Has Visibility', labels=['No', 'Yes'])

plt.subplot(1, 2, 2)
sns.countplot(
    data=df,
    x='Blockchain_adopted',
    hue='has_visibility',
    palette=['red', 'green']
)
plt.title('Blockchain Adoption vs Visibility Methods')
plt.xlabel('Blockchain Adopted (0=No, 1=Yes)')
plt.ylabel('Count')
plt.legend(title='Has Visibility', labels=['No', 'Yes'])

plt.tight_layout()
plt.show()

# 3. List all unique visibility methods mentioned
print("\n=== All Visibility Methods Mentioned ===")
all_methods = pd.concat([df[col] for col in visibility_cols])
unique_methods = all_methods.dropna().unique()
print(unique_methods)




barrier_columns = [
    'further_adoption_issues_Financial_Constraints',
    'further_adoption_issues_Technical_Challenges',
    'further_adoption_issues_Regulatory_Issues',
    'further_adoption_issues_Uncertainty_about_ROI'
]

# Summarize number of "1"s (indicating presence of the barrier)
barrier_counts = df[barrier_columns].sum().sort_values(ascending=False)

# Convert to DataFrame for plotting
barrier_summary = pd.DataFrame({
    'Barrier': barrier_counts.index,
    'Count': barrier_counts.values
})

# Print summary
print("=== Future Adoption Barriers Summary ===")
print(barrier_summary)

# Plot as bar chart
plt.figure(figsize=(8, 6))
sns.barplot(
    data=barrier_summary,
    x='Count',
    y='Barrier',
    palette='Blues_d'
)
plt.title('Future Adoption Barriers')
plt.xlabel('Number of Respondents')
plt.ylabel('Barrier')
plt.tight_layout()

# Show plot
plt.show()

# Explode settings: separate slices slightly
explode = [0.05] * len(barrier_counts)  # explode all slices equally

# Define light color palette
colors = ['#c6dbef', '#9ecae1', '#6baed6', '#3182bd']

# Plot pie chart
plt.figure(figsize=(14, 14))
plt.pie(
    barrier_counts,
    labels=barrier_counts.index,
    autopct='%1.1f%%',
    startangle=140,
    colors=colors,
    explode=explode,
    wedgeprops={'edgecolor': 'black', 'linewidth': 1}
)

plt.title('Future Adoption Barriers (Exploded Pie Chart)', fontsize=14)
plt.axis('equal')  # Ensures the pie chart is circular
plt.tight_layout()

# Show plot
plt.show()